package com.auemall.controller;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.auemall.constant.ApplicationConstant;
import com.auemall.service.ItemParamService;
import com.auemall.utils.EUDataGridResult;
import com.auemall.vo.AttributeVO;
import com.auemall.vo.BaseRespVO;


/**
 * 商品规格参数模板管理Controller
 * <p>Title: ItemParamController</p>
 * <p>Description: </p>
 * 
 *
 */
@Controller
@RequestMapping("/item/v1/param")
public class ItemParamController {
    
    private Logger logger = Logger.getLogger(ItemParamController.class);

	@Autowired
	private ItemParamService itemParamService;
	
	@RequestMapping("/selectattr")
    @ResponseBody
    private EUDataGridResult selectAttributeList(@RequestBody AttributeVO req,int page, int rows) {
	    EUDataGridResult respVO;
        try{
            respVO = itemParamService.selectAttributeList(req, page, rows);
        }catch(Exception e){
            respVO = new EUDataGridResult();
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("查询属性列表异常（/item/v1/param/selectattr）："+e.getMessage());
            logger.error(respVO.getMsg());
        }
        return respVO;
    }
	
	@RequestMapping("/insertattr")
    @ResponseBody
    private BaseRespVO insertAttribute(@RequestBody AttributeVO req) {
	    BaseRespVO respVO;
        try{
            respVO = itemParamService.insertAttribute(req);
        }catch(Exception e){
            respVO = new BaseRespVO();
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("添加属性异常（/item/v1/param/insertattr）："+e.getMessage());
            logger.error(respVO.getMsg());
        }
        return respVO;
    }
	
	@RequestMapping("/updateattr")
    @ResponseBody
    private BaseRespVO updateAttribute(@RequestBody AttributeVO req) {
        BaseRespVO respVO;
        try{
            respVO = itemParamService.updateAttribute(req);
        }catch(Exception e){
            respVO = new BaseRespVO();
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("修改属性异常（/item/v1/param/updateattr）："+e.getMessage());
            logger.error(respVO.getMsg());
        }
        return respVO;
    }
	
	@RequestMapping("/deleteattr")
    @ResponseBody
    private BaseRespVO deleteAttribute(@RequestBody String id) {
        BaseRespVO respVO;
        try{
            respVO = itemParamService.deleteAttribute(id);
        }catch(Exception e){
            respVO = new BaseRespVO();
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("删除属性异常（/item/v1/param/deleteattr）："+e.getMessage());
            logger.error(respVO.getMsg());
        }
        return respVO;
    }
	
	@RequestMapping("/insertattrvalue")
    @ResponseBody
    private BaseRespVO insertAttributeValue(@RequestBody AttributeVO req) {
        BaseRespVO respVO;
        try{
            respVO = itemParamService.insertAttributeValue(req);
        }catch(Exception e){
            respVO = new BaseRespVO();
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("添加属性值异常（/item/v1/param/insertattrvalue）："+e.getMessage());
            logger.error(respVO.getMsg());
        }
        return respVO;
    }
	
	@RequestMapping("/deleteattrvalue")
    @ResponseBody
    private BaseRespVO deleteAttributeValue(@RequestBody String id) {
        BaseRespVO respVO;
        try{
            respVO = itemParamService.deleteAttributeValue(id);
        }catch(Exception e){
            respVO = new BaseRespVO();
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("删除属性值异常（/item/v1/param/deleteattrvalue）："+e.getMessage());
            logger.error(respVO.getMsg());
        }
        return respVO;
    }
	
	/**
	 * 
	 * @Title: selectAttrByProductId 
	 * @Description: TODO(查询与指定商品关联的属性值)
	 * @param req
	 * @param page
	 * @param rows
	 * @return EUDataGridResult  
	 * @author WangChao
	 */
	@RequestMapping("/selectattrbyproductid")
	@ResponseBody
	private EUDataGridResult selectAttrByProductId(@RequestBody AttributeVO req,int page, int rows){
	    EUDataGridResult respVO;
	    try{
	        respVO = itemParamService.selectAttrByProductId(req, page, rows);
	    }catch(Exception e){
	        respVO = new EUDataGridResult();
	        respVO.setResult(ApplicationConstant.RESULT_FALTURE);
	        respVO.setMsg("查询与指定商品关联的属性值异常（/item/v1/param/selectattrbyproductid）："+e.getMessage());
	        logger.error(respVO.getMsg());
	    }
	    return respVO;
	}
	
	
}
